<?php
class ControllerDelimanKhachhang extends Controller
{
	private $error = array();
   	function __construct() 
	{
		$listchek = $this->getListCheck();
		if(in_array($this->getMethod(),$listchek))
		{
			if(!$this->user->hasPermission($this->getRoute(), $this->getMethod()))
			{
				$this->response->redirect("?route=common/permission");
			}
		}
		
		$this->data['trangthai'] = array(
							'dagiao' => 'Đã giao',
							'chuagiao' => 'Chưa giao',
							'huy' => 'Hủy',
						);
		
		$this->load->model("common/control");
	 	$this->load->model("deliman/khachhang");
		$this->load->model("deliman/khuvuc");
		$this->load->model("deliman/nhanvien");
		$this->data['khuvucs'] = array();
		$this->model_deliman_khuvuc->getTree(0, $this->data['khuvucs']);
	}
	
	public function index()
	{
		$this->getList();
	}
	
	public function insert()
	{
    	$this->getForm();
	}
	
	public function update()
	{
		$this->data['haspass'] = false;
		$this->data['readonly'] = 'readonly="readonly"';
		$this->data['class'] = 'readonly';
		$this->getForm();	
  	}
	
	public function thongtindatbao()
	{
		$this->data['haspass'] = false;
		$this->data['readonly'] = 'readonly="readonly"';
		$this->data['class'] = 'readonly';	
		$this->getFormThongtindatbao();

		
  	}
	
	public function getFormThongtindatbao()
	{
			$this->load->model("common/control");		

		
		if ((isset($this->request->get['khachhangid'])) ) 
		{
      		$this->data['item'] = $this->model_deliman_khachhang->getItem($this->request->get['khachhangid']);			
    	}	
		
		$this->id='content';
		$this->template='deliman/thongtindatbao.tpl';
		$this->layout="layout/deliman";
		
		$this->render();	
	}
	
	public function xemthongtindatbao()
	{
		$khachhangid = $this->request->get['khachhangid'];
		$where = " AND khachhangid = '".$khachhangid."'";
		
		$data = $this->request->get;
		if(trim($data['tungay'])!='')
		{
			$where .= " AND ngayphathanh >= '".$this->date->formatViewDate($data['tungay'])."'";
		}
		if(trim($data['denngay'])!='')
		{
			$where .= " AND ngayphathanh <= '".$this->date->formatViewDate($data['denngay'])."'";
		}
		if(trim($data['dattungay'])!='')
		{
			$where .= " AND ngaydat >= '".$this->date->formatViewDate($data['dattungay'])."'";
		}
		if(trim($data['datdenngay'])!='')
		{
			$where .= " AND ngaydat <= '".$this->date->formatViewDate($data['datdenngay'])."'";
		}
		if(trim($data['loaibaoid'])!='')
		{
			$where .= " AND loaibaoid = '".$data['loaibaoid']."'";
		}
		$where.= " Order by ngayphathanh desc ";
		$rows = $this->model_deliman_khachhang->getThongTinDatBaoList($where);
		$this->data['datas'] = array();
		
		$this->load->model("common/control");		
		$this->load->model("deliman/loaibao");
		$this->data['loaibaos'] = $this->model_deliman_loaibao->getList();
		$this->data['loaibao'] = $this->model_common_control->getDataCombobox($this->data['loaibaos'], "tenloaibao", "loaibaoid", $select);
		
		if ((isset($this->request->get['khachhangid'])) ) 
		{
      		$this->data['item'] = $this->model_deliman_khachhang->getItem($this->request->get['khachhangid']);			
    	}

		
		//Page
		$page = $this->request->get['page'];		
		$x=$page;		
		$limit = 20;
		$total = count($rows); 
		// work out the pager values 
		$this->data['pager']  = $this->pager->pageLayout($total, $limit, $page); 
		
		$pager  = $this->pager->getPagerData($total, $limit, $page); 
		$offset = $pager->offset; 
		$limit  = $pager->limit; 
		$page   = $pager->page;
		for($i=$offset;$i < $offset + $limit && count($rows[$i])>0;$i++)
		//for($i=0; $i <= count($this->data['datas'])-1 ; $i++)
		{
			$this->data['datas'][$i] = $rows[$i];
			$this->load->model("deliman/kybao");
			$kybao = $this->model_deliman_kybao->getItem($this->data['datas'][$i]['kybaoid']);
			$this->data['datas'][$i]['giaban'] = $kybao['giaban'] ;
			
			$khachhang = $this->model_deliman_khachhang->getItem($this->data['datas'][$i]['khachhangid']);
			$this->data['datas'][$i]['hoten'] = $khachhang['hoten'];
			$this->data['datas'][$i]['diachi'] = $khachhang['diachi'];
			
			$this->data['datas'][$i]['text_edit'] = "Edit";
			
		}
		
		$this->id='content';
		$this->template='deliman/khachhang_xemthongtindatbao.tpl';
		$this->layout="layout/deliman";
		
		$this->render();
	}
	
	public function updatethongtindatbao()
	{
		$data = $this->request->post;
		$data['soluongdat'] = $this->string->toNumber($data['soluongdat']);
		if($this->validateDatBao($data))
		{
			
			
			
			$this->model_deliman_khachhang->updateThongTinDatBao($data['id'],'soluongdat',$data['soluongdat']);
			
			$arr = array(
						'error'=>'',
						'id'=>$data['id'],
						'soluongdat' => $this->string->numberFormate($data['soluongdat'])
						);
			$this->data['output'] = json_encode($arr);
			
		}
		else
		{
			foreach($this->error as $item)
			{
				$this->data['output'] .= $item;
			}
			$arr = array(
					'error'=>$this->data['output']
					
					);
			$this->data['output'] = json_encode($arr);
		}
		
		$this->id='content';
		$this->template='common/output.tpl';
		$this->render();
	}
	
	private function validateDatBao($data)
	{
		if($data['soluongdat']<= 0)
		{
			$this->error['soluongdat'] = "Bạn chưa nhập số lượng đặt báo không hợp lệ";	
		}
		if (count($this->error)==0) {
	  		return TRUE;
		} else {
	  		return FALSE;
		}
	}
	
	public function delete() 
	{
		$listkhachhangid=$this->request->post['delete'];
		if(count($listkhachhangid))
		{
			foreach($listkhachhangid as $khachhangid)
			{
				$this->model_deliman_khachhang->delete($khachhangid);	
			}
			$this->data['output'] = "true";
		}
		$this->id="content";
		$this->template="common/output.tpl";
		$this->render();
  	}
	
	private function getList() 
	{
		$data = $this->request->get;
		if($data['hoten']!="")
		{
			$where.=" AND hoten like '%".urldecode($data['hoten'])."%'";
		}
		if($data['khuvuc']!="")
		{
			$khuvus = array();
			$this->model_deliman_khuvuc->getTree($data['khuvuc'], $khuvus);
			$arrkhuvucid = $this->string->matrixToArray($khuvus,'khuvucid');
			$data_cbkhachhang = $this->model_deliman_khachhang->getList(" AND khuvucid in ( '".implode("','",$arrkhuvucid)."')");

			$arr_khachhangid = $this->string->matrixToArray($data_cbkhachhang,"khachhangid");
			$where .= " AND khachhangid in ('".implode("','",$arr_khachhangid)."')";
		}
		
		//lay user dang nhap
		$nhanvienphutrachkhuvucid = $this->user->getNhanVien();
		$nhanvien = $this->model_deliman_nhanvien->getItem($nhanvienphutrachkhuvucid);
		if($nhanvien['loainhanvienid'] == 2)
		{
			//tim ra nhung khu vuc do nhan vien dang nhap phu trach
			$wherephutrach = " AND nhanvienphutrachid ='".$nhanvienphutrachkhuvucid."' AND tinhtrang <> 'deleted'";
			$arrphutrachkhuvuc = $this->model_deliman_nhanvien->getPhuTrachKhuVucList($wherephutrach);
			$arrkhuvucphutrachid = $this->string->matrixToArray($arrphutrachkhuvuc,'khuvucid');
			//lay danh sach khu vuc con
			$this->data['khuvucs']=array();
			
			$dskhuvucid=$arrkhuvucphutrachid;
			foreach($dskhuvucid as $khuvucid)
			{
				$dskv=array();
				$this->model_deliman_khuvuc->getTree($khuvucid,$dskv);
				foreach($dskv as $kv)
				{
					array_push($this->data['khuvucs'], $kv);
				}
				//print_r($this->data['dskhuvuc']);
			}
			$this->data['khuvucs'];
			//lay danh sach khu vuc cua khach hang
			$arrkhuvuckhachhangid = $this->string->matrixToArray($this->data['khuvucs'],'khuvucid');
			//lay danh sach khach hang trong khu vuc
			$where .= " AND khuvucid in ( '".implode("','",$arrkhuvuckhachhangid)."')";
		}
		$this->data['insert'] = $this->url->http('deliman/khachhang/insert');
		$this->data['delete'] = $this->url->http('deliman/khachhang/delete');		
		
		$this->data['datas'] = array();
		$rows = $this->model_deliman_khachhang->getList($where);
		//Page
		$page = $this->request->get['page'];		
		$x=$page;		
		$limit = 20;
		$total = count($rows); 
		// work out the pager values 
		$this->data['pager']  = $this->pager->pageLayout($total, $limit, $page); 
		
		//khai bao bien thong bao tim kiem
		$this->data['thongbaotimkiem'] = 0;
		
		$pager  = $this->pager->getPagerData($total, $limit, $page); 
		$offset = $pager->offset; 
		$limit  = $pager->limit; 
		$page   = $pager->page;
		for($i=$offset;$i < $offset + $limit && count($rows[$i])>0;$i++)
		//for($i=0; $i <= count($this->data['datas'])-1 ; $i++)
		{
			$this->data['thongbaotimkiem']++;
			$this->data['datas'][$i] = $rows[$i];
			//tính tổng số lượng báo đã đặt của khách hàng
			$soluongdat = $this->model_deliman_khachhang->sumSoLuongBaoDat($this->data['datas'][$i]['khachhangid']);
			$this->data['datas'][$i]['soluongdat'] = $soluongdat['soluongdat'];
			
			$this->data['datas'][$i]['link_edit'] = $this->url->http('deliman/khachhang/update&khachhangid='.$this->data['datas'][$i]['khachhangid']);
			$this->data['datas'][$i]['link_order'] = $this->url->http('deliman/khachhang/thongtindatbao&khachhangid='.$this->data['datas'][$i]['khachhangid']);
			$this->data['datas'][$i]['text_edit'] = "Edit";
			$this->data['datas'][$i]['text_order'] = "Đặt báo";	
			$this->data['datas'][$i]['link_vieworder'] = $this->url->http('deliman/khachhang/xemthongtindatbao&khachhangid='.$this->data['datas'][$i]['khachhangid']);
		}
		$this->data['refres']=$_SERVER['QUERY_STRING'];
		$this->id='content';
		$this->template="deliman/khachhang_list.tpl";
		$this->layout="layout/deliman";
		
		$this->render();
	}
	
	
	private function getForm()
	{
		$this->load->model("common/control");
		$this->load->model('deliman/khuvuc');
		
		

		
		if ((isset($this->request->get['khachhangid'])) ) 
		{
      		$this->data['item'] = $this->model_deliman_khachhang->getItem($this->request->get['khachhangid']);
			$this->data['khuvuc'] = $this->model_deliman_khuvuc->getItem($this->data['item']['khuvucid']);	
    	}
		
		//cb Tinh thanh
		$data_cbtinhthanh = $this->model_deliman_khuvuc->getList(" AND loaikhuvuc = 'tinhthanh'");
		
		$this->data['cbtinhthanh'] = $this->model_common_control->getDataCombobox($data_cbtinhthanh, "tenkhuvuc", "khuvucid", $this->data['khuvuc']['tinhthanhid']);
		
		
		$this->id='content';
		$this->template='deliman/khachhang_form.tpl';
		$this->layout="layout/deliman";
		
		$this->render();
	}
	
	//public function get
	
	public function getkhuvuc()
	{
		$this->load->model("common/control");
		$this->load->model('deliman/khuvuc');
		
		$tinhthanhid = $this->request->get['id'];
		$loaikhuvuc =  $this->request->get['loai'];
		$select =  $this->request->get['select'];
		$data_cbquanhuyen = $this->model_deliman_khuvuc->getList(" AND loaikhuvuc = '".$loaikhuvuc."' AND khuvuccha = '".$tinhthanhid."' ORDER BY tenkhuvuc ");
		
		
		$this->data['output'] = $this->model_common_control->getDataCombobox($data_cbquanhuyen, "tenkhuvuc", "khuvucid", $select);
		
		$this->template='common/output.tpl';

		$this->render();
	}
	
	public function savethongtindatbao()
	{
		$data = $this->request->post;
		
		$this->load->model("deliman/kybao");
		$this->load->model("deliman/loaibao");
		if($this->validateThongTinDatBao($data))
		{
			foreach($data['soluongdat'] as $key => $val)
			{
				$datbao['khachhangid'] = $data['khachhangid'];
				$datbao['kybaoid'] = $data['kybaoid'][$key];
				$kybao = $this->model_deliman_kybao->getItem($datbao['kybaoid']);
				$loaibao = $this->model_deliman_loaibao->getItem($kybao['loaibaoid']);
				
				$datbao['tenkybao'] = $kybao['tenkybao'];
				$datbao['loaibaoid'] = $kybao['loaibaoid'];
				$datbao['tenloaibao'] = $loaibao['tenloaibao'];
				$datbao['soluongdat'] = $this->string->toNumber($data['soluongdat'][$key]);
				$datbao['sokybao'] = $kybao['sokybao'];
				$datbao['ngaydat'] = $this->date->formatViewDate($data['ngaydat'][$key]);
				$datbao['ngayphathanh'] = $kybao['ngayphathanh'];
				$datbao['trangthai'] = "chuagiao";
				
				$this->model_deliman_khachhang->saveThongTinDatBao($datbao);	
				$this->data['output'] = "true";	
			}
		}
		else
		{
			foreach($this->error as $item)
			{
				$this->data['output'] .= $item."<br>";
			}
		}

		
		
		$this->id='content';
		$this->template='common/output.tpl';
		$this->render();
	}
	
	private function validateThongTinDatBao($data)
	{
		
		if (count($data['soluongdat']) == 0) 
		{
			
      		$this->error['soluongdat'] = "Bạn chưa nhập thông tin đặt báo";
    	}
		else
		{
			foreach($data['soluongdat'] as $soluong)
				if($soluong == 0)
					$this->error['soluongdat'] = "Số lượng báo đặt phải lớn hơn 0";	
		}
		if (count($this->error)==0) {
	  		return TRUE;
		} else {
	  		return FALSE;
		}	
	}
	
	public function save()
	{
		$data = $this->request->post;
		
		if($this->validateForm($data))
		{
			if($data['khachhangid']=="")
			{
				$this->model_deliman_khachhang->insert($data);	
			}
			else
			{
				$this->model_deliman_khachhang->update($data);	
			}
			
			$this->data['output'] = "true";
		}
		else
		{
			foreach($this->error as $item)
			{
				$this->data['output'] .= $item."<br>";
			}
		}
		$this->id='content';
		$this->template='common/output.tpl';
		$this->render();
	}
	
	private function validateForm($data)
	{	
		if (trim($data['hoten']) == "") 
		{
      		$this->error['hoten'] = "Bạn chưa nhập tên khách hàng";
    	}
		
		if (trim($data['khuvucid']) == "") 
		{
      		$this->error['khuvucid'] = "Bạn chưa chọn tên đường";
    	}
		/*if (trim($data['sonha']) == "") 
		{
      		$this->error['sonha'] = "Bạn chưa nhập số nhà";
    	}*/
		
		if (count($this->error)==0) {
	  		return TRUE;
		} else {
	  		return FALSE;
		}
	}
	//Cac ham xu ly tren form
	
}
?>